const express = require("express")
const jwt = require("jsonwebtoken")
const app = express()

const username_DB = 'lf'
const password_DB = '123456'
const jwtScret = "加密内容"

app.use(express.json())

app.post('/login',(req,res) =>{
    if(req.body.username == username_DB && req.body.password == password_DB){
        const token = jwt.sign(
            {name:"lf"},
            jwtScret,
            {algorithm:"HS256",expiresIn:10000}
        )
        res.send(token)
    }else{
        res.send("用户或密码错误")
    }
});

app.post("/vip",(req,res)=>{
    jwt.verify(req.body.token,jwtScret,(err,decoded) =>{
        if(err){
            res.send("用户或密码错误")
        }else{
            res.send(`welcome ${decoded.name}`);
        }
    })
})

app.listen(5000,()=>{console.log("5000 port Running...")})